﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Schema;

namespace Config.Core.Instructions
{
    public class DeleteInstruction : IInstruction
    {
        #region Properties
        /// <summary>
        /// Gets or sets the XML comment node for the instruction.
        /// </summary>
        /// <value>The XML comment node.</value>
        public XComment XComment { get; set; }

        /// <summary>
        /// Gets or sets the XPath of the element to be changed.
        /// </summary>
        /// <value>The XPath of the element.</value>
        [InstructionParam(Name = "xpath", IsOptional = false)]
        public string XPath { get; set; }
        #endregion

        /// <summary>
        /// Runs the instruction against the XML Document.
        /// </summary>
        /// <param name="xDoc">The XML document.</param>
        public void Run(XDocument xDoc)
        {
            var elements = xDoc.XPathSelectElements(XPath);
            foreach (var item in elements)
                item.Remove();

            XComment.Remove();
        }
    }
}
